#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct student
{
	char name[15];
	char surname[15];
	long int id;
	int mt1;
	int mt2;
	int lab;
	int final;

};

void main()
{
	int size=0;
	struct student *newStudent;
	struct student **arry;
	int mt1,mt2,lab,final;
	int id;
	int sum[4] = {0};
	FILE *ptr;


	printf("enter students number: ");
	scanf("%d",&size);

	arry = (student**)malloc(size*sizeof(struct student*));

	if( (ptr =  fopen("students.txt","rb")) == NULL )// check open file
	{
		printf("File could not be opened\n" );
	}
	
	else
	{
		for(int i = 0; i<size ; i++)
		{
			newStudent = (struct student*)malloc(sizeof(struct student));
			// read data from file to struct
			fscanf( ptr, "%s %s %d %d %d %d %d", newStudent->name, newStudent->surname, &id, &mt1, &mt2, &lab, &final);
			newStudent->id = id;
			newStudent->mt1 = mt1;
			newStudent->mt2 = mt2;
			newStudent->lab = lab;
			newStudent->final = final;
			arry[i] = newStudent;
			
		}
		fclose(ptr);// close file
	}
	
	for(int m=0; m<size; m++)// for print
	{
		printf("%s %s %d ",arry[m]->name, arry[m]->surname, arry[m]->id);
		printf("%d %d %d %d\n",arry[m]->mt1, arry[m]->mt2, arry[m]->lab, arry[m]->final);
		
		sum[0] = sum[0] + arry[m]->lab;
		sum[1] = sum[1] + arry[m]->mt1;
		sum[2] = sum[2] + arry[m]->mt2;
		sum[3] = sum[3] + arry[m]->final;
		
	}
	
	printf("\n\nAverage of midterm 1: %0.2f", (float)sum[1]/size);
	printf("\nAverage of midterm 2: %0.2f", (float)sum[2]/size);
	printf("\nAverage of labs     : %0.2f", (float)sum[0]/size);
	printf("\nAverage of final    : %0.2f\n", (float)sum[3]/size);

}